package com.example.apiproject.service.impl;

import com.example.apiproject.entity.GroupMembers;
import com.example.apiproject.dao.GroupMembersDao;
import com.example.apiproject.service.GroupMembersService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * (GroupMembers)表服务实现类
 *
 * @author makejava
 * @since 2023-09-23 13:40:57
 */
@Service("groupMembersService")
public class GroupMembersServiceImpl implements GroupMembersService {
    @Resource
    private GroupMembersDao groupMembersDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public GroupMembers queryById(Integer id) {
        return this.groupMembersDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param groupMembers 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<GroupMembers> queryByPage(GroupMembers groupMembers, PageRequest pageRequest) {
        long total = this.groupMembersDao.count(groupMembers);
        return new PageImpl<>(this.groupMembersDao.queryAllByLimit(groupMembers, pageRequest), pageRequest, total);
    }
    /**
     * 通过user ID查询其加入的群组
     * @param id
     * @return
     */
    @Override
    public List<String> queryByUserId(String id){
        return this.groupMembersDao.queryByUserId(id);
    }
    /**
     * 新增数据
     *
     * @param groupMembers 实例对象
     * @return 实例对象
     */
    @Override
    public GroupMembers insert(GroupMembers groupMembers) {
        this.groupMembersDao.insert(groupMembers);
        return groupMembers;
    }

    /**
     * 修改数据
     *
     * @param groupMembers 实例对象
     * @return 实例对象
     */
    @Override
    public GroupMembers update(GroupMembers groupMembers) {
        this.groupMembersDao.update(groupMembers);
        return this.queryById(groupMembers.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.groupMembersDao.deleteById(id) > 0;
    }
}
